package com.dxl.web.common.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.dxl.common.cache.CacheClient;
import com.dxl.common.model.RespData;
import com.dxl.common.utils.Utils;
import com.dxl.entity.common.BaseEntity;
import com.dxl.service.common.BaseService;

/**
 * 控制器基类，封装通用操作
 * @author 邓小林
 */
public abstract class BaseController {
	private static final Logger LOG = Logger.getLogger(BaseController.class);
	@Autowired
	protected CacheClient cache;
	@Qualifier("baseService")
	@Autowired
	protected BaseService baseService;
	
	@InitBinder
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {  
	     
	}
	
	public RespData del(Class<? extends BaseEntity> clazz, String[] ids) {
		if (Utils.isNull(clazz) || Utils.isEmpty(ids)) {
			return RespData.instance(RespData.FAIL);
		} else {
			try {
				this.baseService.del(clazz, ids);
				return RespData.instance(RespData.SUCCESS);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				return RespData.instance(RespData.FAIL);
			}
		}
	}
	
	public RespData state(Class<? extends BaseEntity> clazz, String[] ids, Integer state) {
		if (Utils.isNull(clazz) || Utils.isEmpty(ids) || Utils.isNull(state)) {
			return RespData.instance(RespData.FAIL);
		} else {
			try {
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("state", state);
				this.baseService.update(clazz, ids, params);
				return RespData.instance(RespData.SUCCESS);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				return RespData.instance(RespData.FAIL);
			}
		}
	}
}
